import { configureStore } from '@reduxjs/toolkit'
import storage from 'redux-persist/lib/storage'
import { persistStore, persistReducer } from 'redux-persist'

// 设置存储的一些信息
const persistConfig = {
  key: 'higo-react-ts-2306',
  storage // 默认存储到localStorage
}

// 导入reducer
import user from './modules/user'

/**
 * persistConfig：配置
 * user：到底存储哪个模块
 */
const persistedReducer = persistReducer(persistConfig, user)

const store = configureStore({
  reducer: {
    user: persistedReducer
  },
  // 解决了序列化问题
  middleware: getDefaultMiddleware =>
    getDefaultMiddleware({ serializableCheck: false })
})

// 创建持久化存储器
const persistor = persistStore(store)

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>

export { store, persistor }
